﻿using HPIT.RentHouse.Common;
using HPIT.RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HPIT.RentHouse.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly ICitiesService _citiesService;
        private readonly IHouseService _houseService;
        public HomeController(ICitiesService citiesService, IHouseService houseService)
        {
            _citiesService = citiesService;
            _houseService = houseService;
        }

        public ActionResult Index(int cityId = 0)
        {
            var list = _citiesService.GetList();
            ViewBag.CityList = list;
            if (cityId > 0)
            {
                ViewBag.cityId = cityId;
                ViewBag.DefaultCity = list.Where(e => e.Id == cityId).FirstOrDefault().Name;
            }
            else
            {
                ViewBag.cityId = list.FirstOrDefault().Id;
                ViewBag.DefaultCity = list.FirstOrDefault().Name;
            }
            return View();
        }

        [HttpPost]
        public ActionResult LoadMore(int pageIndex, int cityId)
        {
            var list = _houseService.GetList(cityId, pageIndex, 6);
            foreach (var item in list)
            {
                if (!string.IsNullOrEmpty(item.FirstThumbUrl))
                {
                    item.FirstThumbUrl = CommonHelper.GetServerIP() + item.FirstThumbUrl;
                }
            }
            return Json(list);
        }
    }
}